package com.systemsjr.mpf.account;

import java.io.Serializable;
import java.util.Date;

import android.net.Uri;

import com.systemsjr.mpf.db.AccountInstanceContentProvider;

public class AccountInstance implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7658457936623454456L;

	// THE CONTENT URI TO OUR PROVIDER
	public static final Uri CONTENT_URI = Uri.parse("content://"
			+ AccountInstanceContentProvider.AUTHORITY + "/accountInstance");

	// MIME TYPE FOR GROUP OF ACCOUNTS
	public static final String CONTENT_TYPE = "vnd.android.cursor.dir/com.systemsjr.mpf.accountInstance";

	// MIME TYPE FOR SINGLE ACCOUNT
	public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/com.systemsjr.mpf.accountInstance";

	public static final int DESCRIPTION_PATH_POSITION = 1;

	public static final String TABLE_NAME = "ACCOUNT_INSTANCE";
	public static final String ID = "_id";
	public static final String INSTANCE = "INSTANCE";
	public static final String BUDGET = "BUDGET";
	public static final String START_DATE = "START_DATE";
	public static final String CLOSE_DATE = "CLOSE_DATE";
	public static final String ACCOUNT_FK = "ACCOUNT_FK";
	public static final String SPENT = "SPENT";
	
	public static final String[] PROJECTIONS = { AccountInstance.ID,
		AccountInstance.START_DATE, AccountInstance.CLOSE_DATE,
		AccountInstance.INSTANCE, AccountInstance.BUDGET };
	public static String DATE_FORMAT = "yyyy-MM-dd";

	private Long id;
	private String instance;
	private double budget;
	private Date startDate;
	private Date closeDate;
	private Account account;
	private double spent;

	public AccountInstance() {
		id = 0L;
		budget = 0;
		spent = 0;
	}

	public AccountInstance(long id, String instance, double budget,
			Date startDate, Date closeDate, Account account) {
		this.id = id;
		this.instance = instance;
		this.budget = budget;
		this.startDate = startDate;
		this.closeDate = closeDate;
		this.account = account;
	}

	public void setSpent(double spent) {
		this.spent = spent;
	}

	public double getSpent() {
		return this.spent;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getInstance() {
		return instance;
	}

	public void setInstance(String instance) {
		this.instance = instance;
	}

	public double getBudget() {
		return budget;
	}

	public void setBudget(double budget) {
		this.budget = budget;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getCloseDate() {
		return closeDate;
	}

	public void setCloseDate(Date closeDate) {
		this.closeDate = closeDate;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public static String createTable() {

		return "CREATE TABLE " + TABLE_NAME + " (" + ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + INSTANCE
				+ " TEXT NOT NULL UNIQUE," + BUDGET + " DOUBLE NOT NULL,"
				+ START_DATE + " DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
				+ CLOSE_DATE + " DATETIME," + ACCOUNT_FK + " BIGINT NOT NULL)";
	}

	public static String listQuery(Long id) {

		return "select " + INSTANCE + ", " + BUDGET + ", "
				+ "(SELECT sum(amount) FROM EXPENSE WHERE EXPENSE.ACCOUNT_INSTANCE_FK = ACCOUNT_INSTANCE._id) SPENT from "
				+ TABLE_NAME + " where ACCOUNT_INSTANCE._id = " + id;

	}
}
